package vip.specimen.system.controller.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.SaveMethodEnum;
import vip.specimen.system.datasource.entity.SystemSaveConfigEntity;
import vip.specimen.system.datasource.validation.SystemSaveConfigAliOssValidation;
import vip.specimen.system.datasource.validation.SystemSaveConfigQiNiuValidation;
import vip.specimen.system.datasource.validation.SystemSaveConfigTencentValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemSaveConfigService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/8 17:49
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "配置管理")
@RestController
@RequestMapping(value = "/config/save")
public class SaveController {
    @Resource
    private SystemSaveConfigService systemSaveConfigService;

    @GetMapping
    @RequiresPermissions(value = "config:save:select")
    @ApiOperation(value = "查看存储配置", notes = "获取存储配置信息")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看存储配置")
    public JsonResult<SystemSaveConfigEntity> getConfig() {
        return JsonResult.success(this.systemSaveConfigService.getConfig());
    }

    @PostMapping(value = "qiNiu")
    @RequiresPermissions(value = "config:save:update")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新存储配置")
    @ApiOperation(value = "更新七牛云配置", notes = "根据实体更新七牛云配置信息")
    public JsonResult<SystemSaveConfigEntity> update(@RequestBody @Valid SystemSaveConfigQiNiuValidation qiNiu) {
        return JsonResult.success(this.systemSaveConfigService.update(qiNiu));
    }

    @PostMapping(value = "aliOss")
    @RequiresPermissions(value = "config:save:update")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新存储配置")
    @ApiOperation(value = "更新阿里OSS配置", notes = "根据实体更新阿里OSS配置信息")
    public JsonResult<SystemSaveConfigEntity> update(@RequestBody @Valid SystemSaveConfigAliOssValidation ossValidation) {
        return JsonResult.success(this.systemSaveConfigService.update(ossValidation));
    }

    @PostMapping(value = "tencent")
    @RequiresPermissions(value = "config:save:update")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新存储配置")
    @ApiOperation(value = "更新腾讯云存储配置", notes = "根据实体更新腾讯云存储配置信息")
    public JsonResult<SystemSaveConfigEntity> update(@RequestBody @Valid SystemSaveConfigTencentValidation tencentValidation) {
        return JsonResult.success(this.systemSaveConfigService.update(tencentValidation));
    }

    @GetMapping(value = "saveMethod")
    @ApiOperation(value = "查看存储方式", notes = "获取全部的存储方式")
    @RequiresPermissions(value = "config:save:select:saveMethod")
    public JsonResult<ArrayList<HashMap<String, Object>>> getSaveMethod() {
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (SaveMethodEnum save : SaveMethodEnum.values()) {
            HashMap<String, Object> map = new HashMap<>(3);
            map.put("methodId", save.getMethodId());
            map.put("methodName", save.getMethodName());
            map.put("name", save.name());
            list.add(map);
        }
        return JsonResult.success(list);
    }
}
